Tutoial - Part 1 TutoRial - Part 1

Marine Ecosystem Dynamics - 2024

Author

Kinlan M.G. Jan

New script

As seen during the presentation, we will keep track of our progress. We thus need to open a new script following one of the option below.

flowchart LR
  A[1. File] --> B[2. New File]
  B --> C[3. R script]
flowchart LR
  A["⌘/Ctlr + ⇧ + N"]

syntax R syntax

is a programming language that use a simplified syntax. In this section, we will explore how to write a script and execute it. R is a programming language that use a simplified syntax. In this section, we will explore how to write a script and execute it.

But first some syntax information:

  • Everything after # is considered as a comment and will not be executed. It is very important to write what we are doing, so we do not get lost next time we open our scripts.
# 2 + 2 will not work because of the #
2 + 2 # We should then annotate our script like this
#> [1] 4
  • Several lines of code can be written in one line but must be separated by a semicolon
2 + 2
#> [1] 4
3 * 2
#> [1] 6

# This can also be written as follow:
2 + 2 ; 3 * 2
#> [1] 4
#> [1] 6
  • In we can name any object using =, <-, -> or assign In R we can name any object using =, <-, -> or assign
c(1, 2, 3, 4) -> my_first_vector
my_vector <- c(1, 2, 3, 4)
my_function = function(x){x + 2}
assign("x", c(2, 3, 4, 5))
  • == is a logical function that can be translated as is equal to, contrarily is not equal to is written !=
2 + 2 == 4
#> [1] TRUE
3 * 2 == 4
#> [1] FALSE
3 * 2 != 4
#> [1] TRUE

Exercises

Using a new script, do these calculations: Using a new R script, do these calculations:

  • \ 2^7
2^7
#> [1] 128
  • \ cos(\pi)
?cos()
?pi()
cos(pi)
#> [1] -1
  • The sum of all number from 1 to 100

Operations can take place for an entire vector

vector <- seq(from = 1, to = 100, by = 1) # Create a vector from 1 to 100
sum(vector) # Calculate the sum
#> [1] 5050

Create a parameter x1 that equals to 5 and a parameter x2 that equals to 10

x1 <- 5 ; x2 <-  10
  • Is \ 2* x1 equal to x2?
2 * x1 == x2
#> [1] TRUE

Functions

As seen during the lecture, works with functions that can: As seen during the lecture, R works with functions that can:

  • Already be implemented in base Already be implemented in base R
  • Comming from another package
  • Created by the user

We will see these three examples in this section, but first it is important to remember that the typical structure of a function is function(argument1, ...).

Fortunately helps us to remember what are the needed arguments: Fortunately R helps us to remember what are the needed arguments:

  • Using help() or ?
help(topic = "sin")
?sin
  • Using example
example(sum)
#> 
#> sum> ## Pass a vector to sum, and it will add the elements together.
#> sum> sum(1:5)
#> [1] 15
#> 
#> sum> ## Pass several numbers to sum, and it also adds the elements.
#> sum> sum(1, 2, 3, 4, 5)
#> [1] 15
#> 
#> sum> ## In fact, you can pass vectors into several arguments, and everything gets added.
#> sum> sum(1:2, 3:5)
#> [1] 15
#> 
#> sum> ## If there are missing values, the sum is unknown, i.e., also missing, ....
#> sum> sum(1:5, NA)
#> [1] NA
#> 
#> sum> ## ... unless  we exclude missing values explicitly:
#> sum> sum(1:5, NA, na.rm = TRUE)
#> [1] 15

For the functions that comes from external packages, we first need to install the new packages. The most common way to do so is by executing install.packages("Package_Name"). Then when we want to load the functions, we start the script by executing library(Package_Name).

Finally, if we really do not find a suitable function in a package, we can create your functions following this general structure, but this will not be covered in this tutorial:

my_function <- function(<argument1>, <argument2>, ...){
  <here comes the definition of my function>
  return(<output of the definition>)
}

Exercises

- What is the function log() doing and from were does this function come from (base , other packages)? - What is the function log() doing and from were does this function come from (base R, other packages)?

?log() #It takes the natural logarithm of the value, it comes from base R
log(10) 
  • What are the mandatory arguments for the function plot()
?plot() # the coordinates points x and y are needed
  • Is there help associated with the functions from a loaded package?

The function ggplot() comes from the package ggplot2

library(ggplot2)
?ggplot # Yes, there is also help for the imported functions
Optional exercises
  • Create a function that print Hello World! when executing it
Hello <- function(){
  cat("Hello World!") # print("Hello World!") works too
}
Hello()
#> Hello World!
  • Create a function that multiply the input by 4
multiplyeR <- function(x, y = 4){
  return(x * y)
}
multiplyeR(x = 2) # It works with values
#> [1] 8
multiplyeR(x = seq(1, 3, 1)) # But also vectors
#> [1]  4  8 12